//
//  MSCommentsViewController.m
//  MinShengBank
//
//  Created by chen wei on 13-10-14.
//  Copyright (c) 2013年 Hq. All rights reserved.
//

#import "MSCommentsViewController.h"
#import "MSCommentsCell.h"
#import "MSWriteCommentViewController.h"
#import "RequestServerDelegate.h"
#import "RequestServer.h"

@interface MSCommentsViewController ()<MSCommentFinishedDelegete>
@property(nonatomic, retain)NSString*     contentID;
@property(nonatomic, retain)NSMutableArray*     listData;
@property(nonatomic, retain)UITableView*     tableView;
@property(nonatomic, retain)UIView*     toolBar;
@end

@implementation MSCommentsViewController
- (id)initWithContentId:(NSString*)contentId
{
    if ([super init] != nil) {
        self.contentID = [[NSString alloc] initWithString:contentId];
    }
    return self;
}
#pragma mark - uitableview
-(void)initTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height-40) style:UITableViewStylePlain];
    [self.tableView setDelegate:self];
	[self.tableView setDataSource:self];
    
	self.tableView.backgroundColor = [UIColor whiteColor];
	self.tableView.backgroundView.alpha = 0.5;
	self.tableView.autoresizingMask = AutoresizingFull;
    self.tableView.delegate = self;
	[self.view addSubview:self.tableView];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self initTableView];
    
    self.listData = [[NSMutableArray alloc] init];
    
    self.toolBar = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.height-40, self.view.width, 40)];
    self.toolBar.backgroundColor = [UIColor colorWithHexString:@"e9e9e9"];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setFrame:CGRectMake(12, 6, 28, 28)];
    [backButton setImage:[UIImage imageNamed:@"goback.png"] forState:UIControlStateNormal];
    backButton.backgroundColor = [UIColor clearColor];
    [backButton addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    [self.toolBar addSubview:backButton];
    
    UIButton *textFieldButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [textFieldButton setFrame:CGRectMake(52, 6, 220, 28)];
    [textFieldButton setImage:[UIImage imageNamed:@"saytext.png"] forState:UIControlStateNormal];
    textFieldButton.backgroundColor = [UIColor clearColor];
    [textFieldButton addTarget:self action:@selector(writeComment) forControlEvents:UIControlEventTouchUpInside];
    [self.toolBar addSubview:textFieldButton];
    
    [self.view addSubview:self.toolBar];
    
    NSDictionary *para = [NSDictionary dictionaryWithObjectsAndKeys:self.contentID,@"newsId",nil];
    [[RequestServer instance] doActionAsync:0 withAction:@"com.xguanjia.client.action.AnnounceAction$getCommentList" withParameters:para withDelegate:self];

    
}







- (void)requestFinished:(NSNumber *)code withMessage:(NSString *)message withData:(id)data
{
    if (code == 0) {
        [self.listData removeAllObjects];
        [self.listData addObjectsFromArray:data];//[data objectForKey:@"comments"]];
        [self.tableView reloadData];
    } else {
        ShowMessage(@"获取评论失败！");
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([self.listData count] == 0) {
        return  1;
    }else{
        return [self.listData count];
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    MSCommentsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (nil == cell) {
        cell = [[MSCommentsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //cell.Backimage = @"cancel@2x.png";
    if ([self.listData count] == 0) {
        UILabel *number = [[UILabel alloc]initWithFrame:CGRectMake((self.view.frame.size.width - 100)/2, 5, 100, 25)];
        number.backgroundColor = [UIColor clearColor];
        number.textAlignment = NSTextAlignmentLeft;
        number.text = @"暂无评论";
        number.font = [UIFont systemFontOfSize:18.0];
        [cell addSubview:number];
    }else{
        cell.titled = [[self.listData objectAtIndex:indexPath.row] objectForKey:@"userName"];
//        cell.subtitle = [[self.listData objectAtIndex:indexPath.row] objectForKey:@"createDate"];
        cell.commentText = [[self.listData objectAtIndex:indexPath.row] objectForKey:@"content"];
    }

    
    return cell;
}

#pragma mark -- 按钮事件 --
- (void)goBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)writeComment
{
    MSWriteCommentViewController *viewController = [[MSWriteCommentViewController alloc] initWithContentId:self.contentID];
    viewController.delegate = self;
//    [self presentModalViewController:viewController animated:YES];
    [self presentViewController:viewController animated:YES completion:^(void) {}];
}

- (void)commentWriteFinished
{
    NSDictionary *para = [NSDictionary dictionaryWithObjectsAndKeys:self.contentID,@"newsId",nil];
    [[RequestServer instance] doActionAsync:0 withAction:@"com.xguanjia.client.action.AnnounceAction$getCommentList" withParameters:para withDelegate:self];
}
@end
